package com.itheima.service.mongo.SoulTestImpl;

import cn.hutool.core.collection.CollectionUtil;
import com.itheima.domain.SoulTest.QuestionNaire;
import com.itheima.domain.SoulTest.ReportState;
import com.itheima.domain.SoulTest.SoulResult;
import com.itheima.domain.SoulTest.UserSoulScore;
import com.itheima.service.SoulTest.QuestionNaireService;
import org.apache.dubbo.config.annotation.DubboService;
import org.bson.types.ObjectId;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Sort;
import org.springframework.data.mongodb.core.MongoTemplate;
import org.springframework.data.mongodb.core.query.Criteria;
import org.springframework.data.mongodb.core.query.Query;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;

@DubboService
public class QuestionNaireServiceImpl implements QuestionNaireService {
    @Autowired
    private MongoTemplate mongoTemplate;

    @Override
    public List<QuestionNaire> findQuestionNaireList() {
        List<QuestionNaire> questionNaires = mongoTemplate.findAll(QuestionNaire.class);
        return questionNaires;
    }


    @Override
    public String setUserScore(int questionNaireNumber, Long userId, Integer score) {
//        UserSoulScore userSoulScore = new UserSoulScore();
        //封装人格代号和对应的id
        List<SoulResult> soulResultList = mongoTemplate.findAll(SoulResult.class);
        HashMap<String, String> map = new HashMap<>();
        for (SoulResult soulResult : soulResultList) {
            if (soulResult.getConclusion().startsWith("猫头鹰")) {
                map.put("猫头鹰", soulResult.getId().toHexString());
            }
            if (soulResult.getConclusion().startsWith("白兔")) {
                map.put("白兔", soulResult.getId().toHexString());
            }
            if (soulResult.getConclusion().startsWith("狐狸")) {
                map.put("狐狸", soulResult.getId().toHexString());
            }
            if (soulResult.getConclusion().startsWith("狮子")) {
                map.put("狮子", soulResult.getId().toHexString());
            }
        }
        //查询做题分别做了多少分
        Query query = new Query(Criteria.where("userId").is(userId));
        UserSoulScore userSoulScore = mongoTemplate.findOne(query, UserSoulScore.class);
        if (userSoulScore == null) {
            userSoulScore = new UserSoulScore();
        }
        userSoulScore.setUserId(userId);


        switch (questionNaireNumber) {
            case 1: {
                if (score >= 56) {
                    String shizi = map.get("狮子");
                    userSoulScore.setFirstResuldId(shizi);
                } else if (score >= 41) {
                    String huli = map.get("狐狸");
                    userSoulScore.setFirstResuldId(huli);
                } else if (score >= 21) {
                    String baitu = map.get("白兔");
                    userSoulScore.setFirstResuldId(baitu);

                } else if (score >= 0) {
                    String maotou = map.get("猫头鹰");
                    userSoulScore.setFirstResuldId(maotou);
                }
                userSoulScore.setFirstScore(score);
                // state.setUserSoulState(1);
                mongoTemplate.save(userSoulScore);
                //   mongoTemplate.save(state);
                return userSoulScore.getFirstResuldId();
            }

            case 2: {
                if (score >= 56) {
                    String shizi = map.get("狮子");
                    userSoulScore.setSecondResuldId(shizi);
                } else if (score >= 41) {
                    String huli = map.get("狐狸");
                    userSoulScore.setSecondResuldId(huli);
                } else if (score >= 21) {
                    String baitu = map.get("白兔");
                    userSoulScore.setSecondResuldId(baitu);

                } else if (score >= 0) {
                    String maotou = map.get("猫头鹰");
                    userSoulScore.setSecondResuldId(maotou);
                }
                userSoulScore.setSecondScore(score);

                //  state.setUserSoulState(2);
                mongoTemplate.save(userSoulScore);
                //   mongoTemplate.save(state);
                return userSoulScore.getSecondResuldId();
            }

            case 3: {
                if (score >= 56) {
                    String shizi = map.get("狮子");
                    userSoulScore.setThirdResuldId(shizi);
                } else if (score >= 41) {
                    String huli = map.get("狐狸");
                    userSoulScore.setThirdResuldId(huli);
                } else if (score >= 21) {
                    String baitu = map.get("白兔");
                    userSoulScore.setThirdResuldId(baitu);

                } else if (score >= 0) {
                    String maotou = map.get("猫头鹰");
                    userSoulScore.setThirdResuldId(maotou);

                }
                userSoulScore.setThirdScore(score);
                // state.setUserSoulState(3);
                //    mongoTemplate.save(state);
                mongoTemplate.save(userSoulScore);

                return userSoulScore.getThirdResuldId();
            }
        }


        return null;
    }

    @Override
    public UserSoulScore findUserSoulScore(Long userId) {
        Query query = new Query(Criteria.where("userId").is(userId));
        UserSoulScore userSoulScore = mongoTemplate.findOne(query, UserSoulScore.class);
        if (userSoulScore == null) {
            userSoulScore = new UserSoulScore();
        }
        return userSoulScore;
    }

    @Override
    public SoulResult findReport(String id, Long userId) {

        SoulResult soulResult = mongoTemplate.findById(new ObjectId(id), SoulResult.class);


        return soulResult;
    }

    @Override
    public List<Long> findSimilarUser(Long userId, Integer score, Integer table) {
        //再找十个分数相近的
        System.out.println("userId-" + userId + "-score-" + score + "-table-" + table);
//        Query query = new Query(Criteria.where("userId").is(userId));
        List<UserSoulScore> soulScoreList = mongoTemplate.findAll(UserSoulScore.class);
        if (soulScoreList.size() <= 1) {
            return null;
        }
        List<Long> idList = new ArrayList<>();
//        System.out.println("soulScoreList--" + soulScoreList);
        List<UserSoulScore> gtList = new ArrayList<>();
        List<UserSoulScore> ltList = new ArrayList<>();

        switch (table) {
            case 1: {
                Query gt = new Query(Criteria.where("firstScore").gte(score)).with(Sort.by(Sort.Order.asc("firstScore"))).skip(0).limit(5);
                Query lt = new Query(Criteria.where("firstScore").lte(score)).with(Sort.by(Sort.Order.desc("firstScore"))).skip(0).limit(5);

                gtList = mongoTemplate.find(gt, UserSoulScore.class);
                ltList = mongoTemplate.find(lt, UserSoulScore.class);
                break;
            }

            case 2: {
                Query gt = new Query(Criteria.where("secondScore").gte(score)).with(Sort.by(Sort.Order.asc("secondScore"))).skip(0).limit(5);
                Query lt = new Query(Criteria.where("secondScore").lte(score)).with(Sort.by(Sort.Order.desc("secondScore"))).skip(0).limit(5);

                gtList = mongoTemplate.find(gt, UserSoulScore.class);
                ltList = mongoTemplate.find(lt, UserSoulScore.class);
                break;

            }
            case 3: {
                Query gt = new Query(Criteria.where("thirdScore").gte(score)).with(Sort.by(Sort.Order.asc("thirdScore"))).skip(0).limit(5);
                Query lt = new Query(Criteria.where("thirdScore").lte(score)).with(Sort.by(Sort.Order.desc("thirdScore"))).skip(0).limit(5);

                gtList = mongoTemplate.find(gt, UserSoulScore.class);
                ltList = mongoTemplate.find(lt, UserSoulScore.class);
                break;

            }

        }
//        System.out.println("gtList" + gtList);
//        System.out.println("ltList" + ltList);

        if (CollectionUtil.isNotEmpty(gtList)) {
            for (UserSoulScore userSoulScore : gtList) {
                Long findId = userSoulScore.getUserId();

                System.out.println("执行添加gtList" + gtList);
                idList.add(findId);
                System.out.println(idList);

            }
        }
        if (CollectionUtil.isNotEmpty(ltList)) {
            for (UserSoulScore userSoulScore : ltList) {
                Long findId = userSoulScore.getUserId();

                System.out.println("执行添加ltList" + ltList);
                idList.add(findId);
                System.out.println(idList);

            }
        }

        return idList;
    }

    @Override
    public String saveReportState(ReportState reportState) {
        //先查询有没有
        System.out.println("saveReportState--");
        Long userId = reportState.getUserId();
        Integer reportNum = reportState.getReportNum();

        Query query = new Query(Criteria.where("userId").is(userId).and("reportNum").is(reportNum));
        ReportState reportState1 = mongoTemplate.findOne(query, ReportState.class);
        System.out.println("reportState1--" + reportState1);
        if (reportState1 == null) {
            reportState.setId(new ObjectId());
        } else {
            //执行修改
            reportState.setId(reportState1.getId());
        }

        mongoTemplate.save(reportState);
        System.out.println("saveReportState---reportState-" + reportState);
        return reportState.getId().toHexString();
    }

    @Override
    public ReportState findReportState(String id) {
        ReportState reportState = mongoTemplate.findById(new ObjectId(id), ReportState.class);
        return reportState;
    }

    @Override
    public List<ReportState> findReportStateByUserId(Long userId) {
        Query query = new Query(Criteria.where("userId").is(userId)).with(Sort.by(Sort.Order.asc("reportNum")));
        List<ReportState> reportStates = mongoTemplate.find(query, ReportState.class);

        return reportStates;
    }

}
